<?php

namespace app\admin\controller\widget;

use app\admin\controller\AuthController;
use services\storage\OSS;
use think\facade\Route as Url;
use services\UploadService as Upload;
use services\JsonService as Json;
use services\UtilService as Util;
use services\FormBuilder as Form;
use app\admin\model\system\SystemAttachmentCategory as Category;
use app\admin\model\system\SystemAttachment as SystemAttachmentModel;


/**
 * @title 附加控制器
 * Class Images
 * @package app\admin\controller\widget
 */
class Images extends AuthController
{
    /**
     * 附件列表
     * @return string
     */
    public function index()
    {
        $pid = request()->param('pid');
        if ($pid === NULL) {
            $pid = session('pid') ? session('pid') : 0;
        }
        session('pid', $pid);
        $this->assign('pid', $pid);
////       //TODO 分类标题
//       $typearray = Category::getAll();
//       $this->assign(compact('typearray'));
//       $this->assign(SystemAttachmentModel::getAll($pid));
        return $this->fetch('widget/images');
    }

    /**获取分类
     * @param string $name
     */
    public function get_image_cate($name = '')
    {
        return Json::successful(Category::getAll($name));
    }

    /**获取图片列表
     *
     */
    public function get_image_list()
    {
        $where = Util::getMore([
            ['page',1],
            ['limit',18],
            ['pid',0]
        ]);
        return Json::successful(SystemAttachmentModel::getImageList($where));
    }

    /**
     * 图片管理上传图片
     * @return \think\response\Json
     */
    public function upload()
    {
        $pid = input('pid')!= NULL ?input('pid'):session('pid');
        $upload_type = $this->request->get('upload_type',0);
        try{
            $path = make_path('attach',2,true);
            $res = Upload::instance()->setUploadPath($path)->setAutoValidate(true)
                ->setUploadType($upload_type)->image('file');
            if(is_object($res) && $res->status === false){
                $info = array(
                    'code' =>400,
                    'msg'  =>'上传失败：'.$res->error,
                    'src'  =>''
                );
            }else if(is_string($res)){
                $info = array(
                    'code' =>400,
                    'msg'  =>'上传失败：'.$res,
                    'src'  =>''
                );
            }else if(is_array($res)){
                $res['dir'] = str_replace('\\','/',$res['dir']);
                $res['thumb_path'] = str_replace('\\','/',$res['thumb_path']);
                SystemAttachmentModel::attachmentAdd($res['name'],$res['size'],$res['type'],$res['dir'],$res['thumb_path'],$pid,$res['image_type'],$res['time']);
                $info = array(
                    'code' =>200,
                    'msg'  =>'上传成功',
                    'src'  =>$res['dir']
                );
            }
        }catch (\Exception $e){
            $info = [
                'code' =>400,
                'msg'  =>'上传失败：'.$e->getMessage(),
                'src'  =>''
            ];
        }
        echo json_encode($info);
    }
}